from typing import Annotated, AsyncGenerator

from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker

from src.api.providers.stub import Stub
from src.database.services.user import UserService
from src.database.uow import SQLAlchemyUoW
from src.database.uow.uow import build_uow


class InfraProvider:
    def __init__(
        self,
        session_factory: async_sessionmaker[AsyncSession],
    ) -> None:
        self._pool = session_factory

    async def get_uow(self) -> AsyncGenerator[SQLAlchemyUoW, None]:
        async with self._pool() as session:
            uow = build_uow(session)
            yield uow


async def get_user_service(
    uow: Annotated[SQLAlchemyUoW, Depends(Stub(SQLAlchemyUoW))]
) -> UserService:
    yield UserService(uow)
